package CtrLayer;
import java.util.ArrayList;

import DBLayer.SaleDB;
import DBLayer.SaleDBIF;
import ModelLayer.Employee;
import ModelLayer.Event;
import ModelLayer.PartSale;
import ModelLayer.Product;
import ModelLayer.Sale;

/**
 *SaleCtr
 *This class handles methods related to sale and partSales.
 *31/05/2012
 *@author Bager
 *@version 0.1
 */

public class SaleCtr {
	
	private static SaleCtr instance;
	
	/**
	 * Creates a new saleCtr.
	 */
	
	private SaleCtr()
	{
		
	}
	
	/**
	 * Returns a instance of SaleCtr if a SaleCtr already exist, creates a new SaleCtr and returns it if a SaleCtr don't already exist.
	 * @return A instance of SaleCtr. 
	 */
	
	public static SaleCtr getInstance()
	{
		if(instance==null)
        {
            instance = new SaleCtr();
        }
        
		return instance;
	}
	
	/**
	 * Find event if there is a event matching date and time and creates a new sale and puts the event and employee on the sale and returns the sale. 
	 * @param employee The employee doing the sale.
	 * @return A sale with an event if any and a employee
	 */
	
	public Sale createSale(Employee employee)
	{
		EventCtr eventCtr = EventCtr.getInstance();
		Event event = eventCtr.findEvent();
		Sale sale = new Sale(event, employee);
		return sale;
		
	}
	
	/**
	 * Finds the product with matching productId and creates a new partSale with the amount, priceType, found product and event on sale. Puts the partSale
	 * on the sale and calculates the price.
	 * @param productId The productId of the product wished found.
	 * @param amount The amount of the product.
	 * @param priceType The priceType of the partSale.
	 * @param sale The sale to store partSale on. 
	 */
	
	public void addProduct(int productId, int amount, String priceType, Sale sale)
	{
		Product product = null;
		ProductCtr productCtr = ProductCtr.getInstance();
	    
		if(productCtr.findDraughtBeer(productId).getVip1Price() == 0)
		{
			
			product = productCtr.findProduct(productId);
			System.out.println("Found product: " + product);
		
		}
		
		else
		{
			product = productCtr.findDraughtBeer(productId);
			System.out.println("Found draught beer: " + product);
		
		}
		
		PartSale partSale = new PartSale(amount, priceType, product, sale.getEvent());
		sale.addPartSale(partSale);
		endSale(sale);
    }
	
	/**
	 * Calculates total price of the sale. 
	 * @param sale The sale that needs total price calculated.
	 */
	
	public void endSale(Sale sale)
	{
		sale.calculateTotalPrice();
	}
		
	/**
	 * Insert the sale into the database.
	 * @param sale The sale to be inserted into the database.
	 * @throws Exception If sale not inserted correctly.
	 */
	
	public void finalizeSale(Sale sale) throws Exception
	{
		SaleDBIF dbSale = new SaleDB();
		dbSale.createSale(sale);
		downsizeInventory(sale);
	}
	
	/**
	 * Removes the latest partSale in the partSales collection on sale a sale.
	 * @param sale The sale with the partSales collection. 
	 */
	
	public void removePartSale(Sale sale)
	{
		ArrayList<PartSale> partSales = sale.getPartsales();
		partSales.remove(partSales.size() -1);
	}
	
	private void downsizeInventory(Sale sale)
	{
		ProductCtr productCtr = ProductCtr.getInstance();
		ArrayList<PartSale> partSales = sale.getPartsales();
		for(PartSale partSale : partSales)
		{
		  productCtr.downsizeInventory(partSale.getProduct(), partSale.getAmount());
	    }
		
	}
	}
		
		
	


